package com.baixun.gassystem.mapper;

import com.baixun.gasentity.entity.Point;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @program: 上海燃气驾驶舱
 * @description: sys_point mapper接口
 * @author: yesong
 **/
@Mapper
public interface PointMapper extends BaseMapper<Point> {

    /**
     * 查询组织架构
     * @param pointId
     * @return
     */
    @Select("SELECT pointName FROM sys_point WHERE pointId = #{pointId} AND deleted = 0")
    String findPointNameByPointId(Long pointId);

    // 更新现有记录
    @Update("UPDATE sys_point SET department_id = #{departmentId}, department_path = #{departmentPath}, extra_info = #{extraInfo} " +
            "WHERE point_id = #{pointId} AND deleted = 0")
    void updatePoint(Point point);

    // 插入新记录
    @Insert("INSERT INTO sys_point (point_id, department_id, department_path, extra_info) " +
            "VALUES (#{pointId}, #{departmentId}, #{departmentPath}, #{extraInfo})")
    void insertPoint(Point point);


    // 根据点位ID查找记录
    @Select("SELECT * FROM sys_point WHERE point_id = #{pointId} AND deleted = 0")
    Point findPointById(@Param("pointId") Long pointId);


    @Select("SELECT pointId FROM sys_point")
    List<Object> selectObjs();

    @Select("SELECT COUNT(*) AS count\n" +
            "FROM sys_point\n" +
            "WHERE JSON_SEARCH(tags, 'one', '云台监控', NULL, '$[*].tagNames') IS NOT NULL\n" +
            "  AND deleted = 0;\n")
    int selectCloud();

    @Select("SELECT COUNT(*) AS count\n" +
            "FROM sys_point\n" +
            "WHERE JSON_SEARCH(tags, 'one', '普通监控', NULL, '$[*].tagNames') IS NOT NULL\n" +
            "  AND deleted = 0;\n")
    int selectNormal();

    /**
     * 查询点位是云端还是本地
     */

    @Select("SELECT *\n" +
            "FROM sys_point\n" +
            "WHERE pointId = #{pointId}\n" +
            "  AND JSON_SEARCH(tags, 'one', '云端监控', NULL, '$[*].tagNames[*]') IS NOT NULL;")
    String  selectCloud(Long pointId);


}

